import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:get/get.dart';
import 'package:get_demo/pages/download/presentation/controllers/MapDataController.dart';
import 'package:get_demo/pages/download/presentation/views/download_view.dart';
import 'package:latlong2/latlong.dart';

import '../../../../map/misc/tile_providers.dart';
import '../../../../map/widgets/drawer/floating_menu_button.dart';

class HomeMapView extends GetView<MapDataController> {
  static const String route = '/';
  const HomeMapView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // drawer: const MenuDrawer(HomePage.route),
      body: Stack(
        children: [
          FlutterMap(
            options: MapOptions(
              initialCenter: const LatLng(51.5, -0.09),
              initialZoom: 2,
              minZoom: 1,
              maxZoom: 21,
              cameraConstraint: CameraConstraint.contain(
                bounds: LatLngBounds(
                  const LatLng(-90, -180),
                  const LatLng(90, 180),
                ),
              ),
            ),
            children: [
              // localTileLayer,
              openStreetMapTileLayer,
              Obx(() {
                return controller.loadingData.value
                    ? const Center(child: CircularProgressIndicator())
                    : PolygonLayer(
                        polygons: controller.geoJsonParser.polygons,
                      );
              })
            ],
          ),
          const FloatingMenuButton(),
          Container(
              height: 40,
              color: Colors.white,
              child: Row(
                children: [
                  TextButton(
                      onPressed: () {
                        Get.dialog(AlertDialog(
                            title: const Text("群友"),
                            content: const Text("tql")));
                      },
                      child: Text(
                        "地图",
                        style: TextStyle(color: Colors.black, fontSize: 14),
                      )),
                  TextButton(
                      onPressed: () {
                        controller.openDownloadPage();
                      },
                      child: Text(
                        "下载地图",
                        style: TextStyle(color: Colors.black, fontSize: 14),
                      ))
                ],
              )),
          Obx(() {
            return controller.visible.value
                ? PositionedDirectional(
                    end: 0,
                    top: 40,
                    child: DownloadView(),
                  )
                : Container();
          })
        ],
      ),
    );
  }
}
